package com.hd.batch.quartz.detail.job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <h3>partitionjob</h3>
 * <p>DemoQuartzJob</p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2020-09-10 16:46
 **/
@Slf4j
public class DemoQuartzJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        Job job = (Job)jobDataMap.get("job");
        JobLauncher jobLauncher = (JobLauncher) jobDataMap.get("jobLauncher");
        log.info("job :{}",job);
        log.info("jobLauncher :{}",jobLauncher);
        //log.info("jobRegistry :{}",jobRegistry);

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sf.format(new Date());
        try {
            log.info("当前时间 :{},当前执行任务:{}",date,job);
            //
//            String localFileName = "user.txt";
            JobParameters jobParameters = new JobParametersBuilder()
                    .addDate("date", new Date())
//                    .addString("fileName", localFileName)
                    .toJobParameters();
            jobLauncher.run(
                    job,jobParameters);
            log.info("**********************job:{}执行完毕****************",job);
        } catch (Exception e) {
            log.error("发生异常{}",e);
        }

    }
}
